package com.wondertek.web.controller.sys;


import cn.hutool.json.JSONObject;
import com.wondertek.common.base.Result;
import com.wondertek.common.config.WxConfig;
import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.sys.req.SysConfigReq;
import com.wondertek.service.entity.sys.SysConfigPo;
import com.wondertek.service.service.sys.ISysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/config/")
@Tag(name = "系统配置管理")
public class SysConfigController {

    @Resource
    private WxConfig wxConfig;

    @Resource
    private ISysConfigService sysConfigService;

    @GetMapping("/wxConfig")
    @Operation(summary = "获取微信配置")
    public Result<JSONObject> wxConfig(){
        JSONObject config = new JSONObject();
        config.set("templateId",wxConfig.getTemplateId());
        return Result.success(config);
    }


    /**
     * 获取系统配置
     *
     * @return
     */
    @GetMapping("/getConfig")
    @Operation(summary = "获取系统配置")
    public Result<SysConfigPo> getConfig(){
        SysConfigPo sysConfigPo = sysConfigService.getConfig();
        return Result.success(sysConfigPo);
    }


    /**
     * 修改系统配置
     *
     * @return
     */
    @PostMapping("/update")
    @Operation(summary = "修改系统配置")
    @AutoLog(category = "sys", handleType = "log.system.config",
            handleNote = "'修改系统配置'")
    @PreAuthorize("@el.check('backend:sys:config:update')")
    public Result<?> update(@RequestBody @Validated SysConfigReq sysConfigReq){
        sysConfigService.update(sysConfigReq);
        return Result.success("update.success");
    }

}
